import cv2 as cv
import os
import numpy as np
import test
import numpy as np
import process
import Home
np.set_printoptions(threshold=np.inf)
import split_numbers

def cv_imread(filePath):
   # cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1) -1是4通道
    cv_img = cv.imdecode(np.fromfile(filePath, dtype=np.uint8),-1)
    ## imdecode读取的是rgb，如果后续需要opencv处理的话，需要转换成bgr，转换后图片颜色会变化
    ##cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR)
    return cv_img


if __name__ == '__main__':

    path="D:\桌面文件\毕设\服务外包大赛数据更新\训练数据\img_train_曲线"
    path="D:\桌面文件\毕设\服务外包大赛数据更新\训练数据\img_train_折线"
    path="D:\Shanmh\DeskTop\实验"
    files=os.listdir(path)
    for file in files:
        cv.waitKey(0)
        cv.destroyAllWindows()
        Home.Image_name=file
        image=cv_imread(path+"/"+file+"/draw.png")
        image = image[:, :, :3]
#准备工作：把白边变黑边
        image_black = image.copy()
        image_fan = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        if np.sum(image_fan[5:10, 5:10]) > 4000:
            image_black[:, :, :] = 255 - image_black[:, :, :]

#step 1：判断横坐标数字位置，确定数字大小
        biaoji_image,left_number_start,left_number_end,zero_mid,down,up,number_high,jamb_up,jamb_down,jamb_left,jamb_right=process.local_number_down(image_black.copy())
        numbers=process.split_numbers(image_black.copy(),left_number_start,left_number_end,number_high)


        # split_numbers.start(image_black,numbers,number_high,left_number_end+2)
        # colour_sub_image=process.colour_sub(image_black.copy(),jamb_up,jamb_down,jamb_left,jamb_right)
        # process.get_augment(image_black.copy(),colour_sub_image.copy())


        image_bin=process.sub_all(image_black.copy(),jamb_up,jamb_down,jamb_left,jamb_right)
        image_zhi=process.zhi_xin(image_bin.copy())

        result=process.result(image_zhi.copy(),numbers,path+"/"+file+"/db.txt",jamb_left,jamb_right)




        # cv.imshow("image_zhi_and_image",cv.add(image_black,cv.cvtColor(image_zhi,cv.COLOR_BAYER_BG2RGB)))
    print("over")




    #最终还是要上下相减加上左右相减
